package com.example.administrator.szb.activity.supei;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.androidkun.xtablayout.XTabLayout;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.HomeActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseFragmentActivity;
import com.example.administrator.szb.bean.SuPeiBuiness;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SuPeiActivity extends MVPBaseFragmentActivity {
    private XTabLayout tabLayout;
    private TextView bar_title;
    List<SuPeiFragment> list = new ArrayList<>();
    List<SuPeiBuiness> busList = HomeActivity.suPeiBuiness;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_su_pei);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("速配项目");
        tabLayout = (XTabLayout)findViewById(R.id.tabLayout);
    }

    @Override
    protected void initEvent() {
        list.add(null);
        initTabLayout("全部");
        initFragment(0);

        tabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                initFragment(tab.getPosition());
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }
    private void initTabLayoutdata(){
        for (int i=0;i<busList.size();i++){
            list.add(null);
            if (busList.get(i).getMold()!=4){
                initTabLayout(busList.get(i).getTitle());
            }

        }
    }
    @Override
    protected void initData() {
        if (busList!=null&&busList.size()>0){
            initTabLayoutdata();
        }else {
            String url = URLAddress.SUPEI_BUINESS;
            DialogUtil.showIsoProgressbar(context,"加载数据中");
            Map<String,Object> map = new HashMap<String, Object>();
            HttpUtils.post(activity,url, map, new OnResultListener() {
                @Override
                public void onSuccess(int requestCode, String str) {
                    DialogUtil.dialog_ios.dismiss();
                    SPUtils.setDatas("supeibuiness",str);
                    busList= JSON.parseArray(str,SuPeiBuiness.class);
                    initTabLayoutdata();
                }

                @Override
                public void onFailure(int requestCode, String str) {

                }
            });
        }
    }
    private void initTabLayout(String business){
        tabLayout.addTab(tabLayout.newTab().setText(business));
    }

    /**
     * 显示Fragment
     * @param i
     */
    public void initFragment(int i) {
        int id = 0;
        if (i!=0){
            id=busList.get(i).getId();
        }
        FragmentTransaction transaction = getFragmentTransaction();
        hideFragment(transaction);
        if (list.get(i)==null){
            SuPeiFragment f = new SuPeiFragment();
            if (i!=0){
                f.setDatas(busList.get(i-1).getId());
            }else {
                f.setDatas(id);
            }


            list.set(i,f);


            transaction.add(R.id.frameLayout,list.get(i));
        }else {
            transaction.show(list.get(i));
        }
        transaction.commitAllowingStateLoss();


    }
    private void hideFragment(FragmentTransaction transaction){
        for (int i=0;i<list.size();i++){
            if (list.get(i)!=null){
                transaction.hide(list.get(i));
            }
        }
    }

    }
